package com.suos.core;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import cn.hutool.core.util.StrUtil;
import com.suos.conf.SuOsProperties;
import com.suos.core.entity.SysInfo;
import com.suos.core.logic.SugarClient;
import com.suos.core.logic.SugarServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.SmartInitializingSingleton;

import javax.annotation.Resource;
import java.util.concurrent.*;

/**
 * 信息注册
 *
 * @author : MisShi
 * @date: 2021-10-21 17:16:59
 */
@Slf4j
public class SugarExecutor implements SmartInitializingSingleton {

    @Resource
    private SugarOshi sugarOshi;

    @Resource(name = "getAsyncExecutor")
    private Executor executor;

    public SuOsProperties suOsProperties;

    public SugarExecutor(SuOsProperties suOsProperties) {
        this.suOsProperties = suOsProperties;
    }

    @Override
    public void afterSingletonsInstantiated() {
        String registerUrl = suOsProperties.getRegisterUrl();
        if (StrUtil.isNotBlank(registerUrl)) {
            SysInfo sysInfo = sugarOshi.getSysInfo();
            log.info("registerUrl:{}", suOsProperties.getRegisterUrl());
            boolean orServer = suOsProperties.isOrServer();
            if (orServer) {
                executor.execute(new SugarServer(suOsProperties));
            }
            boolean orNeed = suOsProperties.isOrNeed();
            if (orNeed) {
                executor.execute(new SugarClient(suOsProperties, sysInfo));
            }
        } else {
            throw new RuntimeException("sugar.oshi.register-url:属性数值异常");
        }

    }

}
